/**
 *  Java Api For Ecco Generated Applications 
 *  
 *  Copyright (C) 2000  by PDTec GmbH 
 *
 * @author  Michael Weirich
 */
package pdtec.ecco.meta;

import java.lang.Exception;
import pdtec.ecco.meta.Application;

/**
 * The class Schema is a proxy for the equivalent Express Schema. 
 * You can access the schemas in the underlying ecco application
 * only by instantiation of Application or InstApplication.
 * 
 * @see  pdtec.ecco.inst.InstApplication
 * @see  pdtec.ecco.meta.Application
 */
public class Schema {
    /**
     * Id of the the related application instance (either Application or InstApplication). Each application instance
     * has a set of Schemas. This relationship is expressed by this reference id.
     *
     * @see pdtec.ecco.meta.Application#getId
     */
    protected int appId;
    
    /**
     * Name of the equivalent Express Schema. This field is used as access key to the internal schema heap.
     */
    protected String name;

    /**
     * The standard constructor is protected since instance creation creation is delegated
     * to the static {@link #get(int,String) get(...) } method.
     *
     * @see #get(int,String)
     *
     */
    protected Schema(){
	super();
    } // constructor
    
    /**
     * The constructor is protected since instance creation of this class is  is delegated
     * to the static {@link #get(int,String) get(...) } method.
     * @param appId the id of the assiociated Application
     * @param name the schema name
     * @see #get(int,String)
     */
    protected Schema(int appId, String name ) {
        super();
        this.appId = appId;
        this.name = name;
    } // constructor 
     
    /**
     * Factory method for instance creation.Returns the specified schema.
     * If this Schema does not exist null will be returned.
     * @param id application id
     * @param name schema name
     * @return schema object or null
     * @exception EccoException in case an error occurs
     */
    static public native synchronized Schema get(int appId, String name) throws EccoException;
       
    /**
     * Removes all existing weak references in the internal heap of the ecco java api.
     * @param appId related application id
     * @param name schema name
     * @exception EccoException in case an error occurs
     */
    protected native synchronized void delete(int appId,  String name) throws EccoException;
    
    /** Returns array of entities defined in this schema.
     * @return array of entities
     * @exception EccoException in case an error occurs
     */
    public native synchronized Type[] getEntities() throws EccoException;
    
    /** Returns array of defined types in this schema.
     * @return array of defined types
     * @exception EccoException in case an error occurs
     */
    public native synchronized Type[] getDefinedTypes() throws EccoException;
    
    /**
     * Returns the schema name. The schema name is assigned when the schema instance
     * is created.
     * @return schema name
     */
    public String getName() {
	return(name);
    } // getName 
    
    /**
     * Returns the id of the associated Application. 
     * @return associated Application id
     */
    public int getAppId() {
	return(appId);
    } // getAppId
    
    /**
     * Finalizes the object and calls delete. 
     * @see #delete
     */   
    protected void finalize() {
	try { 
	    if( appId != -1)
		delete(appId, name);
	} catch( EccoException e) {
	    System.out.println("EccoException" + e.toString() );
	} // 
	
    } // finalize
    
} // class Schema









